/**
 * @author JackGao
 *			email:jackgao.china@gmail.com
 *  date：2016-6-24
 * 
 */
package cn.cuit.ds.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import cn.cuit.ds.domain.Goods;
import cn.cuit.ds.domain.OrderDeatil;
import cn.cuit.ds.domain.Shopper;
import cn.cuit.ds.service.IShopperService;
import cn.cuit.ds.service.impl.ShopperService;
import cn.cuit.ds.utils.OtherUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

@Controller(value="ShopperAction" )
public class ShopperAction extends ActionSupport {

	// ----- 实体对象-----
	Shopper shopper;

	public Shopper getShopper() {
		return shopper;
	}

	public void setShopper(Shopper shopper) {
		this.shopper = shopper;
	}

	Goods goods;

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	
	
	public String ordersId;
	public void setOrdersId(String ordersId) {
		this.ordersId = ordersId;
	}
	
	public String getOrdersId() {
		return ordersId;
	}

	// ----- service-----
	@Resource(name="ShopperService")
	public IShopperService shopperService ;
	public void setShopperService(IShopperService shopperService) {
		this.shopperService = shopperService;
	}

	/**
	 * 文件上传模块
	 */
	private File file;
	private String fileFileName;
	private String fileContentType;

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	/**
	 * 登陆
	 * 
	 * @return 成功返回loginSuccess 不成功js提示
	 */
	public String login() {
		System.out.println(shopperService==null);
		Shopper login = shopperService.login(shopper);
		if (login == null) {
			super.addFieldError("err", "please check your password");
			return "err";
		}
		ActionContext.getContext().getSession().put("shopperInfo", login);
		return "loginSuccess";
	}

	public String register() {
		shopperService.regist(shopper);
		return "register";
	}

	/**
	 * 添加商品
	 * 
	 * @return
	 */
	public String add() {
		// 上传文件，文件名称为s
		String path = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		// String path2="E:\\upload\\";
		int index = fileFileName.lastIndexOf(".");
		String string = fileFileName.substring(index);
		String s = OtherUtils.getUUID() + string;
		// System.out.println(s);
		// System.out.println(path+s);
		File destFile = new File(path, s);
		try {
			FileUtils.copyFile(file, destFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 增加goods的图片属性
		goods.setShopPic("..\\upload" + "\\" + s);
		// goods.setShopPic(path2+s);
		Shopper shopper = (Shopper) ActionContext.getContext().getSession()
				.get("shopperInfo");
		goods.setShoperId(shopper.getShoperId());
		shopperService.addGoods(goods);
		return "add";
	}

	/**
	 * 所有货品的页面、可以删除，增加、查看
	 * 
	 * @return
	 */
	public String viewAllGoods() {
		Shopper shopper = (Shopper) ActionContext.getContext().getSession()
				.get("shopperInfo");
		String id = shopper.getShoperId();
		List<Goods> allGoods = shopperService.getAllGoods(id);
		ServletActionContext.getRequest().setAttribute("allGoods", allGoods);
		return "viewAllGoods";
	}

	/**
	 * 删除某一个货物
	 * 
	 * @return
	 */
	public String delete() {
		shopperService.deleteGoods(goods.getGoodsId());
		return "delete";
	}

	/**
	 * 更新的第一步，先查询
	 * 
	 * @return
	 */
	public String updateView() {
		Goods byId = shopperService.getById(goods.getGoodsId());
		ServletActionContext.getRequest().setAttribute("goodInfo", byId);
		return "updateView";
	}

	/**
	 * 表单提交更新
	 * 
	 * @return
	 */
	public String update() {
		// 如果上传文件才更新
		if (file != null && fileFileName != null) {
			// 图片更新
			String path = ServletActionContext.getServletContext().getRealPath(
					"/upload");
			int index = fileFileName.lastIndexOf(".");
			String string = fileFileName.substring(index);
			String s = OtherUtils.getUUID() + string;
			File destFile = new File(path, s);
			try {
				FileUtils.copyFile(file, destFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			// 增加goods的图片属性
			goods.setShopPic("..\\upload" + "\\" + s);
		}
		shopperService.updateGoods(goods);
		return "update";
	}

	/**
	 * 查看订单详情
	 * 
	 * @return
	 */
	public String viewDetail() {
		Shopper shopper = (Shopper) ActionContext.getContext().getSession()
				.get("shopperInfo");
		String id = shopper.getShoperId();
		List<OrderDeatil> orderDetail = shopperService.getAllOrderDetail(id);
		ServletActionContext.getRequest().setAttribute("orderDetail",
				orderDetail);
		return "viewDetail";
	}

	/**
	 * 登出
	 * 
	 * @return
	 */
	public String logout() {
		ActionContext.getContext().getSession().remove("shopperInfo");
		return "logout";
	}

	public String myDetail(){
		Shopper shopper = (Shopper) ActionContext.getContext().getSession()
				.get("shopperInfo");
		List<Goods> allGoods = shopperService.getAllGoods(shopper.getShoperId());
		ServletActionContext.getRequest().setAttribute("allGoods",
				allGoods);
		return "myDetail";
	}
	
	
	public String changeState(){
		shopperService.changeState(ordersId);
		return "myDetail";
	}
}
